var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    headImg: '',
    nickName: '',
    sex: 0,
    name: '',
    grade: '',
    department: '',
    className: '',
    active: 0
  },
  onLoad: function(options) {
    if (options.phone) {
      this.setData({
        phone: options.phone
      });
    }
    let info = wx.getStorageSync('info');
    if (info) {
      let sex = info.gender == '2' ? 0 : info.gender;
      this.setData({
        headImg: info.avatarUrl,
        nickName: info.nickName,
        sex: sex
      })
    }
    
  },
  isVip() {
    this.setData({
      active: 0
    })
  },
  isVip2() {
    this.setData({
      active: 1
    })
  },
  bindDateChange: function(e) {
    this.setData({
      grade: e.detail.value
    })
  },
  bindName(event) {
    this.setData({
      name: event.detail.value
    });
  },
  bindDepartment(event) {
    this.setData({
      department: event.detail.value
    });
  },
  bindClass(event) {
    this.setData({
      className: event.detail.value
    });
  },
  finishReg: function() {
    let phone = this.data.phone;
    let grade = this.data.grade;
    let name = this.data.name;
    let nickName = this.data.nickName;
    let headImg = this.data.headImg;
    let sex = this.data.sex;
    let className = this.data.className;
    let department = this.data.department;
    let active = this.data.active;
    if (active == 1){
      if (name == '') {
        util.showErrorToast('姓名不能为空');
        return false;
      }

      if (grade == '') {
        util.showErrorToast('学届不能为空');
        return false;
      }

      if (department == '') {
        util.showErrorToast('院系不能为空');
        return false;
      }

      if (className == '') {
        util.showErrorToast('班级不能为空');
        return false;
      }
    }
    

    util.requestPost(api.ToRegUrl, {
      phone: phone,
      headImg: headImg,
      nickName: nickName,
      sex: sex,
      grade: grade,
      name: name,
      department: department,
      className: className
    }).then(function(res) {
      if (res.success) {
        wx.setStorageSync('user', res.data);
        wx.setStorageSync('token', res.data.token);
        wx.switchTab({
          url: '/pages/home/home'
        })

      } else {
        wx.showToast({
          title: res.message
        })
      }
    });
}
})